﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UFSoft.UBF.Util.Log;
using UFIDA.U9.BS.Job.RequestClient;
using UFSoft.UBF.UI.ControlModel;
using UFSoft.UBF.UI.Engine.Builder;
using UFSoft.UBF.UI.WebControlAdapter;
using UFSoft.UBF.UI.IView;
using System.Data;

namespace UFIDA.U9C.Cust.ZL.PlugUI
{
    /// <summary>
    /// 生产订单界面
    /// </summary>
    public class DiscreteMOMainUIFormWebPart : UFSoft.UBF.UI.Custom.ExtendedPartBase
    {
        private UFIDA.U9.MFG.MO.DiscreteMOUIModel.DiscreteMOMainUIFormWebPart part;
        private static ILogger logger = LoggerManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public override void AfterDataLoad(IPart Part)
        {
            base.AfterDataLoad(Part);

        }
        public override void BeforeDataCollect(IPart Part, out bool executeDefault)
        {
            base.BeforeDataCollect(Part, out executeDefault);

        }
        public override void AfterDataBinding(IPart Part)
        {
            base.AfterDataBinding(Part);
        }

        public override void AfterDataCollect(IPart Part)
        {
            base.AfterDataCollect(Part);
        }

        public override void AfterEventProcess(IPart Part, string eventName, object sender, EventArgs args)
        {
            base.AfterEventProcess(Part, eventName, sender, args);
        }

        public override void AfterInit(IPart Part, EventArgs args)
        {
            base.AfterInit(Part, args);
        }
        public override void AfterLoad(IPart Part, EventArgs args)
        {
            base.AfterLoad(Part, args);
        }

        public override void AfterRender(IPart Part, EventArgs args)
        {
            base.AfterRender(Part, args);
        }

        public override void BeforeDataBinding(IPart Part, out bool executeDefault)
        {
            base.BeforeDataBinding(Part, out executeDefault);
            setCustDefault(Part);
        }

        public override void BeforeDataLoad(IPart Part, out bool executeDefault)
        {
            base.BeforeDataLoad(Part, out executeDefault);
        }

        public override void BeforeEventProcess(IPart Part, string eventName, object sender, EventArgs args, out bool executeDefault)
        {
            base.BeforeEventProcess(Part, eventName, sender, args, out executeDefault);
        }
        public override void BeforeInit(IPart Part, EventArgs args)
        {
            base.BeforeInit(Part, args);
        }
        public override void BeforeLoad(IPart Part, EventArgs args)
        {
            base.BeforeLoad(Part, args);
        }
        public override void BeforeRender(IPart Part, EventArgs args)
        {
            base.BeforeRender(Part, args);
        }

        /// <summary>
        /// 设置默认货主组织 和入库组织
        /// </summary>
        private void setCustDefault(IPart Part)
        {
            part = Part as UFIDA.U9.MFG.MO.DiscreteMOUIModel.DiscreteMOMainUIFormWebPart;
            if (part == null)
                return;
            if (this.part.Model.MO.FocusedRecord.DocState == 0 && this.part.Model.MO.FocusedRecord.ID <=0)
            {


                if (UFSoft.UBF.Util.Context.PlatformContext.Current.OrgCode.Length > 3)
                {
                    string orgcode = UFSoft.UBF.Util.Context.PlatformContext.Current.OrgCode.Substring(0, 3);
                    string sql = $"select code,a.id,name from Base_Organization a inner join Base_Organization_trl b on a.id=b.ID where b.SysMLFlag='zh-CN' and a.Code='{orgcode}'";
                    DataSet ds = null;
                    int result = UFSoft.UBF.Util.DataAccess.DataAccessor.RunSQL(UFSoft.UBF.Util.DataAccess.DataAccessor.GetConn(), sql, null, null, true, out ds);
                    if (ds != null && ds.Tables.Count > 0)
                    {
                        DataTable dt = ds.Tables[0];
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            long org = (long)dt.Rows[0]["id"];
                            string code = dt.Rows[0]["code"].ToString();
                            string name = dt.Rows[0]["name"].ToString();
                            this.part.Model.MO.FocusedRecord.OwnerOrg = org;
                            this.part.Model.MO.FocusedRecord.OwnerOrg_Code = code;
                            this.part.Model.MO.FocusedRecord.OwnerOrg_Name = name;
                            //this.part.Model.MO.FocusedRecord.OwnerOrg_Def_Code = code;
                            //this.part.Model.MO.FocusedRecord.OwnerOrg_Def_Name = name;
                            this.part.Model.MO.FocusedRecord.CompleteInOrg = org;
                            this.part.Model.MO.FocusedRecord.CompleteInOrg_Code = code;
                            this.part.Model.MO.FocusedRecord.CompleteInOrg_Name = name;
                        }
                    }
                }


            }
        }

    }
}

